package com.hxw.serviceribbon.service;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

@Service
public class HelloService {

    //通过构造器的方式依赖注入
    private RestTemplate restTemplate;

    @Autowired
    public HelloService(RestTemplate restTemplate){
        this.restTemplate = restTemplate;
    }

    /**
     * 测试访问
     * APPCLIENT 为服务名称
     * @param name
     * @return
     */
    @HystrixCommand(fallbackMethod = "helloError")
    public String helloService(String name){
        return restTemplate.getForObject("http://APPCLIENT/home?name="+name,String.class);
    }

    public String helloError(String name){
        return "hello,"+name+",sorry,error;this server is missing";
    }

}
